//
// Created by longyunbo on 7/21/21.
//

#include "StackValueCollection.h"
#include "../utilties/common.h"
#include "JavaVFrame.h"

StackValueCollection::StackValueCollection(){

};

StackValueCollection::StackValueCollection(int size){
    maxLocals = size;
    locals = new StackValue[size];
};

StackValue* StackValueCollection::getLocal(){
    return locals;
};

void  StackValueCollection::add(int index, StackValue value){
    getLocal()[index] = value;
};

StackValue StackValueCollection::get(int index){
    return getLocal()[index];
};


void StackValueCollection::push(StackValue* value){
    container.push(value);
};
StackValue* StackValueCollection::pop() {
//    StackValue* s =container.pop();
    //top返回栈顶元素 只是返回并不会移除
    StackValue* sv = container.top();
    //pop 移除栈顶元素
    container.pop();
    return sv;
}

void StackValueCollection::pushDouble(double value){
    //麻烦这里直接将Double转换为long 通过两个方法转换
    union {
        double jd;
        int    array[2];
    } x;
    x.jd = value;
    stack<StackValue*> abc;
    push(new StackValue(bt->T_DOUBLE,x.array[0]));
    push( new StackValue(bt->T_DOUBLE,x.array[1]) );
};

double StackValueCollection::popDouble(){
    union {
        double jd;
        int    array[2];
    } value;
    StackValue* value1 = pop();
    StackValue* value2 = pop();
    if ( (long)(value1->getType()) != bt->T_DOUBLE || (long)value2->getType() != bt->T_DOUBLE) {
        INFO_PRINT("校验不通过\n");
        exit(-1);
    }
    value.array[0] =(value2->getIntData());
    value.array[1] =(value1->getIntData());
    return value.jd;
};

void StackValueCollection::pushInt( int val , pvoid* jv  ){
    //frame.getStack().push(new StackValue(BasicType.T_INT, val));
    JavaVFrame* jvf =(JavaVFrame*)jv;
    jvf->getStack()->push( new StackValue( bt->T_INT , val) );
};









